package com.yuwubao.hz_International.services.impl;

import com.yuwubao.hz_International.entities.BannerImgEntity;
import com.yuwubao.hz_International.entities.repository.BannerImgRepository;
import com.yuwubao.hz_International.services.BannerImgService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yangyu on 2018/1/29.
 */
@Service
public class BannerImgServiceImpl implements BannerImgService {

    @Autowired
    private BannerImgRepository bannerImgRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<BannerImgEntity> findAll() {
        return bannerImgRepository.findAll();
    }

    @Override
    public BannerImgEntity save(BannerImgEntity bannerImg) {
        return bannerImgRepository.save(bannerImg);
    }

    @Override
    public BannerImgEntity update(BannerImgEntity bannerImg) {
        BannerImgEntity bannerImgEntity = bannerImgRepository.findOne(bannerImg.getId());
        if (bannerImgEntity != null) {
            ModelMapper model = new ModelMapper();
            BannerImgEntity entity = model.map(bannerImg, BannerImgEntity.class);
            return bannerImgRepository.save(entity);
        }
        return null;
    }

    @Override
    public BannerImgEntity delete(int bannerImgId) {
        BannerImgEntity entity = bannerImgRepository.findOne(bannerImgId);
        if (entity != null) {
            bannerImgRepository.delete(bannerImgId);
            return entity;
        }
        return null;
    }
}
